package com.example.myapplication_4

import android.content.Intent
import android.os.Bundle
import android.widget.*
import androidx.appcompat.app.AppCompatActivity

class LoginActivity : AppCompatActivity() {

    private lateinit var avatarImageView: ImageView
    private lateinit var nameEditText: EditText
    private lateinit var genderRadioGroup: RadioGroup
    private lateinit var studentIdEditText: EditText
    private lateinit var phoneEditText: EditText
    private lateinit var emailEditText: EditText
    private lateinit var hobbyEditText: EditText
    private lateinit var highScoreTextView: TextView
    private lateinit var editInfoButton: Button
    private lateinit var startGameButton: Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main2)

        initViews()
        setupClickListeners()
        loadUserInfo()
    }

    private fun initViews() {
        avatarImageView = findViewById(R.id.avatarImageView)
        nameEditText = findViewById(R.id.nameEditText)
        genderRadioGroup = findViewById(R.id.genderRadioGroup)
        studentIdEditText = findViewById(R.id.studentIdEditText)
        phoneEditText = findViewById(R.id.phoneEditText)
        emailEditText = findViewById(R.id.emailEditText)
        hobbyEditText = findViewById(R.id.hobbyEditText)
        highScoreTextView = findViewById(R.id.highScoreTextView)
        editInfoButton = findViewById(R.id.editInfoButton)
        startGameButton = findViewById(R.id.startGameButton)
    }

    private fun setupClickListeners() {
        editInfoButton.setOnClickListener {
            // 跳转到修改信息界面
            val intent = Intent(this, EditProfileActivity::class.java)
            startActivity(intent)
        }

        startGameButton.setOnClickListener {
            // 跳转到 MainActivity（翻牌游戏）
            val intent = Intent(this, MainActivity::class.java)
            startActivity(intent)
        }

        avatarImageView.setOnClickListener {
            Toast.makeText(this, "点击更换头像", Toast.LENGTH_SHORT).show()
        }
    }

    private fun loadUserInfo() {
        loadSavedUserInfo()
    }

    private fun loadSavedUserInfo() {
        val sharedPreferences = getSharedPreferences("user_info", MODE_PRIVATE)

        // 从SharedPreferences加载数据并显示在界面上
        nameEditText.setText(sharedPreferences.getString("name", ""))
        studentIdEditText.setText(sharedPreferences.getString("studentId", ""))
        phoneEditText.setText(sharedPreferences.getString("phone", ""))
        emailEditText.setText(sharedPreferences.getString("email", ""))

        // 加载性别
        val gender = sharedPreferences.getString("gender", "")
        if (gender == "男") {
            genderRadioGroup.check(R.id.maleRadioButton)
        } else if (gender == "女") {
            genderRadioGroup.check(R.id.femaleRadioButton)
        }

        // 加载爱好
        val hobbies = sharedPreferences.getStringSet("hobbies", setOf())
        if (hobbies != null && hobbies.isNotEmpty()) {
            hobbyEditText.setText(hobbies.joinToString("、"))
        }

        // 关键修改：正确加载最高分
        val highScore = sharedPreferences.getInt("highScore", 0)
        highScoreTextView.text = highScore.toString()
    }

    override fun onResume() {
        super.onResume()
        // 当从游戏页面或修改页面返回时，重新加载用户信息和最高分
        loadSavedUserInfo()
    }
}